home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / files / createdirwcusticon / createdirwcusticon.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  11.0 KB  |  527 lines

  1. /*
  2.     File:        CreateDirWithCustomIcon.c
  3.     
  4.     Description:This snippet demonstrates how to create a directory and how to steal a custom
  5.                 icon from another directory by CatMoving the "Icon\r" file from the other
  6.                 directory.
  7.  
  8.     Author:        VMC
  9.  
  10.     Copyright:     Copyright: © 1996-1999 by Apple Computer, Inc.
  11.                 all rights reserved.
  12.     
  13.     Disclaimer:    You may incorporate this sample code into your applications without
  14.                 restriction, though the sample code has been provided "AS IS" and the
  15.                 responsibility for its operation is 100% yours.  However, what you are
  16.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  17.                 after having made changes. If you're going to re-distribute the source,
  18.                 we require that you make it clear in the source that the code was
  19.                 descended from Apple Sample Code, but that you've made changes.
  20.     
  21.     Change History (most recent first):
  22.                 6/25/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  23.  
  24. */
  25.  
  26.  
  27. #include <StandardFile.h>
  28. #include <Script.h>
  29. #include "CreateDirWCustIcon.h"
  30.  
  31. /*****  globals *****/
  32. Boolean                    gQuitTheApp = false;        /* true = quit program */
  33. /*****    main() *****/
  34.  
  35. void main()
  36. {
  37.     short            sleepTime  =  60;
  38.     EventRecord        myEvent;
  39.  
  40.     
  41.     initApp();                                /* call initialization routine */
  42.     setUpMenus();
  43.     do
  44.     {
  45.         if (WaitNextEvent(everyEvent, &myEvent, sleepTime, NIL))
  46.             switch (myEvent.what)
  47.             {
  48.                 case mouseDown:
  49.                     doMousedown(&myEvent);
  50.                     break;
  51.                     
  52.                 case keyDown:
  53.                 case autoKey:
  54.                     doKey(&myEvent);
  55.                     break;
  56.  
  57.                 case kHighLevelEvent:
  58.                     doHighLevel(&myEvent);
  59.                     break;
  60.  
  61.                 case activateEvt:
  62.                     break;
  63.                 
  64.                 case updateEvt:
  65.                     doUpdate(&myEvent);
  66.                     break; 
  67.                 
  68.                 case diskEvt:
  69.                     break;
  70.             
  71.                 case osEvt:                /*  I don't really care yet. */
  72.                     break;
  73.             }
  74.     }  while (!gQuitTheApp);        
  75.     
  76. }                 /* end of main */
  77.  
  78.  
  79. /*****    initApp() *****/
  80.  
  81. void initApp()
  82. {
  83.     
  84.     /* Memory specific initializations. */
  85.     
  86.     MaxApplZone();                /* grow the heap to its maximum size */
  87.     MoreMasters();                   /* create more master pointers         */
  88.     MoreMasters();
  89.     MoreMasters();
  90.     MoreMasters();
  91.     MoreMasters();
  92.  
  93.     /* Initializing the ROM Managers */
  94.     
  95.     InitGraf((Ptr) &qd.thePort);
  96.     InitFonts();
  97.     InitWindows();
  98.     InitMenus();
  99.     FlushEvents(everyEvent,0);
  100.     TEInit();
  101.     InitDialogs(NIL);
  102.     InitCursor();
  103.     
  104.     if (!checkGestaltFeatures())
  105.         ExitToShell();
  106.     
  107.     /* Install the required Apple Event Handlers */
  108.     if (!installAEHandlers())
  109.         ExitToShell();
  110. }                /* end of initApp() */
  111.  
  112.  
  113.  
  114. /*****  checkGestaltFeatures() *****/
  115. Boolean checkGestaltFeatures()
  116. {
  117.     long        gestaltFeature;
  118.     OSErr        myErr;
  119.  
  120.     myErr = Gestalt(gestaltSystemVersion, &gestaltFeature);     /* which SysVersion present? */
  121.     if (myErr == noErr)
  122.     {
  123.         gestaltFeature = (gestaltFeature >> 8) & 0xf; 
  124.                                         /* shift result over & mask out major version number */
  125.         if (gestaltFeature < 7)         /* This is a System 7+ shell.  We quit otherwise. */
  126.         {
  127.             StopAlert(BADSYSTEMID, nil);
  128.             return(false);
  129.         }
  130.     }
  131.     
  132.     if (myErr == noErr)
  133.     {
  134.         myErr = Gestalt(gestaltQuickdrawVersion, &gestaltFeature);
  135.                                                     /* we want color QD cuz we're spoiled */
  136.         if (myErr == noErr)
  137.         {
  138.             if(gestaltFeature < gestalt32BitQD)
  139.             {
  140.                 StopAlert(BADQUICKDRAWID, nil);
  141.                 return(false);
  142.             }
  143.         }
  144.     }
  145.     
  146.     
  147.     
  148.     if (myErr == noErr)
  149.     {
  150.         myErr = Gestalt(gestaltAppleEventsAttr, &gestaltFeature);
  151.         if (myErr == noErr)
  152.         {
  153.             if (!(gestaltFeature & 0xf))
  154.             {
  155.                 StopAlert(NOAPPLEEVENTS, nil);
  156.                 return(false);
  157.             }
  158.         }
  159.     }
  160.     if (!myErr)        
  161.         return(true);            /* if there was an error we cannot continue */
  162.     else
  163.     {
  164.         return(false);            /* we made it through without a hitch */
  165.     }
  166. }
  167.  
  168. /*****    installAEHandlers *****/
  169. Boolean installAEHandlers()
  170. {
  171.     OSErr        myErr;
  172.     
  173.     myErr = AEInstallEventHandler ( kCoreEventClass, 
  174.                     kAEOpenApplication, NewAEEventHandlerProc(DoOpenAppAE), 0L, false );
  175.     if (myErr == noErr)
  176.         myErr = AEInstallEventHandler ( kCoreEventClass,
  177.                     kAEOpenDocuments, NewAEEventHandlerProc(DoOpenDocAE), 0L, false );
  178.     if (myErr == noErr)
  179.         myErr = AEInstallEventHandler ( kCoreEventClass,
  180.                     kAEPrintDocuments, NewAEEventHandlerProc(DoPrintDocAE), 0L, false );
  181.     if (myErr == noErr)
  182.         myErr = AEInstallEventHandler ( kCoreEventClass,
  183.                     kAEQuitApplication, NewAEEventHandlerProc(DoQuitAppAE), 0L, false );
  184.     if (myErr)
  185.         return(false);
  186.     else
  187.         return(true);
  188. }
  189.  
  190. /*****    setUpMenus() 
  191.             This will put up our menus.  Be sure to read the comments by the
  192.             AppendMenu() and SetItem() calls to see how different options
  193.             behave.        
  194. *****/
  195. void setUpMenus()
  196. {
  197.     short            n;
  198.     MenuHandle        theMenu;
  199.     
  200.     for (n = 0; n < MAXMENUS; n++)
  201.     {
  202.         theMenu = GetMenu(FIRSTMENUID + n);
  203.         if (theMenu)                  
  204.             InsertMenu(theMenu, 0);
  205.     }
  206.  
  207.     theMenu = GetMenuHandle(APPLEMENU);
  208.     if (theMenu)
  209.         AppendResMenu(theMenu, 'DRVR');
  210.     
  211.     DrawMenuBar();
  212.     return;
  213. }                    /* end of setUpMenus()  */
  214.  
  215. /*****    setUpWindow() *****/
  216.  
  217. void setUpWindow( void )
  218. {
  219.     WindowPtr    myWind;
  220.  
  221.     myWind = GetNewCWindow(WINDOWID, nil, PUTINFRONT);
  222.     if (myWind != nil)
  223.     {
  224.         ShowWindow(myWind);
  225.     }
  226. }
  227.  
  228.  
  229. /*****    doMousedown()
  230.             We figure out where the user has clicked the mouse.  If the user
  231.             clicks anywhere but the menu bar, we beep.  Otherwise, we figure
  232.             out which menu they have clicked and dispatch.
  233. *****/
  234. void doMousedown(EventRecord *eventRec)
  235. {
  236.     short        windPart;
  237.     WindowPtr    myWind;
  238.     long        menuResult;
  239.     
  240.     windPart = FindWindow(eventRec->where, &myWind);
  241.     
  242.     switch(windPart)
  243.     {
  244.         case inContent:
  245.             SelectWindow(myWind);
  246.             break;
  247.  
  248.         case inDrag:
  249.             DragWindow( myWind, eventRec->where, &qd.screenBits.bounds );
  250.             break;
  251.  
  252.         case inMenuBar:
  253.             menuResult = MenuSelect(eventRec->where);
  254.             dispatch(menuResult);
  255.             break;
  256.  
  257.         case inSysWindow:
  258.             SystemClick( eventRec, myWind );
  259.             break;
  260.             
  261.         case inGoAway:
  262.             gQuitTheApp = true;
  263.             break;
  264.             
  265.         default:
  266.             break;
  267.     }
  268.     return;
  269. }                 /* end of doMousedown */
  270.  
  271.  
  272. /*****     doKey()
  273.             We ignore keys pressed unless they are accompanied by a 
  274.             command key.  Then we dispatch.
  275. *****/
  276. void doKey(EventRecord *eventRec)
  277. {
  278.     char    keyPressed;
  279.     long    menuResult;
  280.     
  281.     keyPressed = (char) (eventRec->message & charCodeMask);
  282.     
  283.     if((eventRec->modifiers & cmdKey) != 0)
  284.     {
  285.         menuResult = MenuKey(keyPressed);
  286.         dispatch(menuResult);    
  287.     }
  288.     return;
  289. }                 /* end of do_key */
  290.  
  291.  
  292. /*****    doHighLevel() *****/
  293. void doHighLevel(EventRecord *eventRec)
  294. {
  295.     OSErr myErr;
  296.     myErr = AEProcessAppleEvent(eventRec);
  297. }
  298.  
  299.  
  300. pascal OSErr    DoOpenAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  301. {
  302.     #pragma unused(theAppleEvent, reply, refCon)
  303.     
  304.     setUpWindow();
  305.     return(noErr);
  306. }
  307. pascal OSErr    DoOpenDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  308. {
  309.     #pragma unused(theAppleEvent, reply, refCon)
  310.     
  311.     return(noErr);
  312. }
  313. pascal OSErr    DoPrintDocAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  314. {
  315.     #pragma unused(theAppleEvent, reply, refCon)
  316.     
  317.     return(noErr);
  318. }
  319. pascal OSErr    DoQuitAppAE(AppleEvent theAppleEvent, AppleEvent reply, long refCon)
  320. {
  321.     #pragma unused(theAppleEvent, reply, refCon)
  322.     
  323.     gQuitTheApp = true;
  324.     return(noErr);
  325. }
  326.  
  327.  
  328. /***** doUpdate() *****/
  329. void    doUpdate(EventRecord *eventRec)
  330. {
  331.     GrafPtr        savedPort;
  332.     WindowPtr    theWindow;
  333.     theWindow = (WindowPtr) eventRec->message;
  334.     
  335.     GetPort(&savedPort);
  336.     SetPort((GrafPtr) theWindow);
  337.     BeginUpdate(theWindow);
  338.     EndUpdate(theWindow);
  339.     SetPort(savedPort);
  340. }
  341.  
  342.  
  343. /*****     dispatch()
  344.             We determine which menu the user has chosen (either with mouse
  345.             or with command keys) and jump to the routine that handles
  346.             that menu's commands.
  347. *****/
  348. void dispatch(long menuResult)
  349. {
  350.     short        theMenu;            /* menu selected */
  351.     short        theItem;            /* item selected */
  352.     
  353.     theMenu = HiWord (menuResult);        /* menuID selected */
  354.     theItem = LoWord (menuResult);        /* item# selected */
  355.     
  356.     switch (theMenu)
  357.     {
  358.         case APPLEMENU:
  359.             doAppleCmds(theItem);
  360.             break;
  361.  
  362.         case FILEMENU:
  363.             doFileCmds(theItem);
  364.             break;
  365.             
  366.         case EDITMENU:
  367.             break;
  368.  
  369.         case TESTMENU:
  370.             doTestCmds(theItem);
  371.             break;
  372.     }
  373.     HiliteMenu(0);
  374. }                /* end of dispatch */
  375.  
  376.  
  377.  
  378. /*****     doAppleCmds() 
  379.             When the user chooses the "About MyMenuText" item, we display the
  380.             About box.  If the user chooses a DA, we open the DA.
  381. *****/
  382. void doAppleCmds(short theItem)
  383. {    
  384.     MenuHandle        myMenu;
  385.     Str255            name;
  386.     short            dummy;
  387.     
  388.     if(theItem == appleABOUT)
  389.     {
  390.         Alert(ABOUTID, (ModalFilterUPP) NIL);
  391.     }
  392.     else
  393.     {
  394.         myMenu = GetMenuHandle (APPLEMENU);
  395.         if (myMenu)
  396.         {
  397.             GetMenuItemText(myMenu, theItem, name);
  398.             dummy = OpenDeskAcc(name);
  399.         }
  400.     }
  401.     return;
  402. }                 /* end of doAppleCmds */
  403.  
  404.  
  405.  
  406. /*****    doFileCmds() 
  407.             When the user chooses Quit on the File menu, we quit.
  408. *****/
  409. void doFileCmds (short theItem)
  410. {
  411.     switch (theItem)
  412.     {            
  413.         case fileQUIT:
  414.             gQuitTheApp = true;
  415.             break;
  416.     }
  417.     return;
  418. } /* end of doFileCmds */
  419.  
  420.  
  421.  
  422. /*****    doTestCmds()
  423.             When the user chooses the item in the Test menu, they
  424.             will get to choose a folder with a custom icon
  425. *****/
  426. void doTestCmds (short theItem)
  427. {    
  428.     StandardFileReply    reply;
  429.     FSSpec                spec;
  430.     OSErr                err;
  431.     
  432.     switch (theItem)
  433.     {
  434.         case testDOTHETEST:
  435.             //open a folder with a custom icon and choose the "Icon\r" file
  436.             StandardGetFile(nil, -1, nil, &reply);
  437.             
  438.             if (reply.sfGood)
  439.             {
  440.                 //make an FSSpec to the "Icon\r" file
  441.                 err = FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, 
  442.                                     reply.sfFile.name, &spec);
  443.                 if (!err)
  444.                     makeCustomIconFolder(&spec);
  445.             }
  446.             break;            
  447.     }    
  448.     return;
  449. }                /* end of doTestCmds */
  450.  
  451. #define        HAS_CUST_ICON    0x0400
  452. #define     HAS_BEEN_INIT     0x0100
  453.  
  454. void makeCustomIconFolder(FSSpec *iconFileSpec)
  455. {
  456.     OSErr        err;
  457.     FSSpec        foldSpec;
  458.     long        dirID;
  459.     CInfoPBRec    pb;
  460.  
  461.     /*    
  462.         Make an FSSpec for the directory we're creating
  463.         For demo purposes, we'll put this in the root directory
  464.         of the boot volume.
  465.     */
  466.     err = FSMakeFSSpec(-1, fsRtDirID, "\pNew Folder", &foldSpec);
  467.     
  468.     /* Since the folder doesn't exist, we should get fnfErr */
  469.     if (err == fnfErr)
  470.     {
  471.         //create the the directory
  472.         err = FSpDirCreate(&foldSpec, smSystemScript, &dirID);
  473.             
  474.         if (!err)
  475.         {
  476.             pb.dirInfo.ioCompletion = nil;
  477.             pb.dirInfo.ioNamePtr = "\pNew Folder"; // we care about file names
  478.             pb.dirInfo.ioVRefNum = foldSpec.vRefNum;
  479.             pb.dirInfo.ioFDirIndex = 0;    // use ioNamePtr
  480.             pb.dirInfo.ioDrDirID = foldSpec.parID;
  481.             
  482.             err = PBGetCatInfoSync(&pb);
  483.             
  484.             //set the custom icon flag bit in the DInfo record for the new folder
  485.             if (!err)
  486.             {
  487.                 if ( (pb.dirInfo.ioDrUsrWds.frFlags & HAS_CUST_ICON) == 0)
  488.                 {
  489.                     pb.dirInfo.ioDrUsrWds.frFlags |= HAS_CUST_ICON;  
  490.                         /* turn this bit on  */
  491.                     pb.dirInfo.ioDrUsrWds.frFlags &= ~HAS_BEEN_INIT; 
  492.                         /* turn this bit off */
  493.                 }
  494.  
  495.                 /*
  496.                     PBGetCatInfo messes up the dirID field in parameter block
  497.                     so we have to reset it.
  498.                 */
  499.                 pb.dirInfo.ioDrDirID = foldSpec.parID;
  500.                 err = PBSetCatInfoSync(&pb);    // set the hasCustomIcon flag
  501.                 
  502.                 //Move the icon file into the new folder
  503.                 if (!err)
  504.                 {
  505.                     /*
  506.                         Move our invisible Icon file ("\pIcon\r")
  507.                         into our new folder.
  508.                     */
  509.                     err = FSpCatMove(iconFileSpec, &foldSpec);
  510.                     
  511.                     if (!err)
  512.                     {
  513.                         /* 
  514.                             Do a Mod Date Bump to get Finder to update cache
  515.                             Note:  Because the Finder caches information about
  516.                             open windows, you might need to use the BumpDate
  517.                             routine from MoreFiles
  518.                         */
  519.                     }
  520.                 }
  521.             }
  522.         }
  523.  
  524.     }
  525.     
  526.     return;
  527. }